﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class Hotel_Entity : EntityBase<HOTEL>
    {
        public override RESP_Info Add(HOTEL model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into HOTEL(HOTEL_ID,HOTEL_NAME,HOTEL_DES,HOTEL_ADDRESS,HOTEL_WEBSITE,HOTE_DETAIL) values (@HOTEL_ID,@HOTEL_NAME,@HOTEL_DES,@HOTEL_ADDRESS,@HOTEL_WEBSITE,@HOTE_DETAIL)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("HOTEL_ID", model.HOTEL_ID));
                    cmd.Parameters.Add(new SqlParameter("HOTEL_NAME", model.HOTEL_NAME));
                    cmd.Parameters.Add(new SqlParameter("HOTEL_DES", model.HOTEL_DES));
                    cmd.Parameters.Add(new SqlParameter("HOTEL_ADDRESS", model.HOTEL_ADDRESS));
                    cmd.Parameters.Add(new SqlParameter("HOTEL_WEBSITE", model.HOTEL_WEBSITE));
                    cmd.Parameters.Add(new SqlParameter("HOTE_DETAIL", model.HOTE_DETAIL));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加酒店成功</p></h5>";
                    info.Url = "HotelList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加酒店失败</p></h5>";
                    info.Url = "HotelAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加酒店失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(HOTEL model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update HOTEL set HOTEL_NAME=@HOTEL_NAME,HOTEL_DES=@HOTEL_DES,HOTEL_ADDRESS=@HOTEL_ADDRESS,HOTEL_WEBSITE=@HOTEL_WEBSITE,HOTE_DETAIL=@HOTE_DETAIL where HOTEL_ID=@HOTEL_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("HOTEL_ID", model.HOTEL_ID));
                    cmd.Parameters.Add(new SqlParameter("HOTEL_NAME", model.HOTEL_NAME));
                    cmd.Parameters.Add(new SqlParameter("HOTEL_DES", model.HOTEL_DES));
                    cmd.Parameters.Add(new SqlParameter("HOTEL_ADDRESS", model.HOTEL_ADDRESS));
                    cmd.Parameters.Add(new SqlParameter("HOTEL_WEBSITE", model.HOTEL_WEBSITE));
                    cmd.Parameters.Add(new SqlParameter("HOTE_DETAIL", model.HOTE_DETAIL));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑酒店成功</p></h5>";
                    info.Url = "HotelList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑酒店失败</p></h5>";
                    info.Url = "HotelAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑酒店失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(HOTEL hotel)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from HOTEL where HOTEL_ID=@HOTEL_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("HOTEL_ID", hotel.HOTEL_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除酒店成功</p></h5>";
                    info.Url = "HotelList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除酒店失败</p></h5>";
                    info.Url = "HotelList.aspx";

                    Tom.Net.Log.LogManager.Error("删除酒店失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override HOTEL GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select HOTEL_ID,HOTEL_NAME,HOTEL_DES,HOTEL_ADDRESS,HOTEL_WEBSITE,HOTE_DETAIL from HOTEL where HOTEL_ID=@HOTEL_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("HOTEL_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        HOTEL hotel = new HOTEL();
                        if (!reader.IsDBNull(0)) hotel.HOTEL_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) hotel.HOTEL_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) hotel.HOTEL_DES = reader.GetString(2);
                        if (!reader.IsDBNull(3)) hotel.HOTEL_ADDRESS = reader.GetString(3);
                        if (!reader.IsDBNull(4)) hotel.HOTEL_WEBSITE = reader.GetString(4);
                        if (!reader.IsDBNull(5)) hotel.HOTE_DETAIL = reader.GetString(5);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return hotel;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取酒店数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override HOTEL GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        HOTEL hotel = new HOTEL();
                        if (!reader.IsDBNull(0)) hotel.HOTEL_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) hotel.HOTEL_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) hotel.HOTEL_DES = reader.GetString(2);
                        if (!reader.IsDBNull(3)) hotel.HOTEL_ADDRESS = reader.GetString(3);
                        if (!reader.IsDBNull(4)) hotel.HOTEL_WEBSITE = reader.GetString(4);
                        if (!reader.IsDBNull(5)) hotel.HOTE_DETAIL = reader.GetString(5);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return hotel;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取酒店数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<HOTEL> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<HOTEL> hotels = new List<HOTEL>();
                    while (reader.Read())
                    {
                        HOTEL hotel = new HOTEL();
                        if (!reader.IsDBNull(0)) hotel.HOTEL_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) hotel.HOTEL_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) hotel.HOTEL_DES = reader.GetString(2);
                        if (!reader.IsDBNull(3)) hotel.HOTEL_ADDRESS = reader.GetString(3);
                        if (!reader.IsDBNull(4)) hotel.HOTEL_WEBSITE = reader.GetString(4);
                        if (!reader.IsDBNull(5)) hotel.HOTE_DETAIL = reader.GetString(5);

                        hotels.Add(hotel);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return hotels;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取酒店数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}
